// pages/nutrition/nutrition.js
Page({
  data: {
    currentDate: '',
    // 营养分析数据
    nutritionData: {
      protein: 25,
      fat: 30,
      carb: 45
    },
    // 一周营养数据趋势
    weeklyNutritionData: [
      { day: '周一', protein: 20, fat: 35, carb: 45 },
      { day: '周二', protein: 25, fat: 30, carb: 45 },
      { day: '周三', protein: 30, fat: 25, carb: 45 },
      { day: '周四', protein: 25, fat: 30, carb: 45 },
      { day: '周五', protein: 20, fat: 35, carb: 45 },
      { day: '周六', protein: 35, fat: 20, carb: 45 },
      { day: '周日', protein: 25, fat: 30, carb: 45 }
    ],
    // 营养建议
    nutritionAdvice: [
      {
        title: '蛋白质摄入',
        content: '您本周蛋白质摄入较为均衡，建议继续保持。优质蛋白质来源包括鸡蛋、牛奶、瘦肉等。'
      },
      {
        title: '脂肪摄入',
        content: '您的脂肪摄入略高，建议减少油炸食品和高脂肪肉类的摄入，增加鱼类和坚果类食品。'
      },
      {
        title: '碳水化合物',
        content: '您的碳水摄入基本合理，建议选择全谷物、糙米等复杂碳水化合物替代部分精制糖类。'
      }
    ]
  },

  onLoad() {
    console.log('营养分析页面加载');
    this.setCurrentDate();
  },

  onShow() {
    // 页面显示时的逻辑
    this.drawNutritionChart();
  },

  // 设置当前日期
  setCurrentDate() {
    const now = new Date();
    const dateStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    this.setData({
      currentDate: dateStr
    });
  },

  // 绘制营养图表
  drawNutritionChart() {
    const ctx = wx.createCanvasContext('nutritionChart', this);
    const centerX = 150;
    const centerY = 150;
    const radius = 100;
    
    const data = [
      { value: this.data.nutritionData.protein, color: '#22C55E' },
      { value: this.data.nutritionData.fat, color: '#F97316' },
      { value: this.data.nutritionData.carb, color: '#3B82F6' }
    ];
    
    let startAngle = -0.5 * Math.PI;
    data.forEach(segment => {
      const endAngle = startAngle + (segment.value / 100) * (Math.PI * 2);
      
      ctx.beginPath();
      ctx.moveTo(centerX, centerY);
      ctx.arc(centerX, centerY, radius, startAngle, endAngle);
      ctx.closePath();
      
      ctx.setFillStyle(segment.color);
      ctx.fill();
      
      startAngle = endAngle;
    });
    
    // 绘制中心白色圆形
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius * 0.6, 0, Math.PI * 2);
    ctx.setFillStyle('white');
    ctx.fill();
    
    ctx.draw();
  },

  // 底部导航切换
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    
    switch (tab) {
      case 'home':
        wx.redirectTo({
          url: '/pages/index/index'
        });
        break;
      case 'history':
        wx.redirectTo({
          url: '/pages/history/history'
        });
        break;
      case 'plan':
        wx.redirectTo({
          url: '/pages/plan/plan'
        });
        break;
      case 'profile':
        wx.redirectTo({
          url: '/pages/profile/profile'
        });
        break;
      default:
        wx.redirectTo({
          url: '/pages/index/index'
        });
    }
  }
})